define(["jquery","vue",'common'],function ($,Vue,common){
    var Controller = {
        login:function (){
            var app = Vue.createApp({
                data(){
                    return {
                        config:Config,
                        form:{
                            username:"",
                            password:""
                        },
                        rules:{
                            username:[
                                { required: true, message: '请输入账户', trigger: 'blur' },
                            ],
                            password:[
                                { required: true, message: '请输入密码', trigger: 'blur' },
                            ]
                        }
                    }
                },
                methods:{
                    login(){
                        let that = this;
                        this.$refs.formEl.validate((valid) => {
                            if (valid) {
                                console.log(that.form)
                                let load = that.$loading()
                                $.post("login",{row:that.form},function (res){
                                    load.close()
                                    if(res.code==1){
                                        that.$message({
                                            message: res.msg?res.msg:"操作成功",
                                            type: 'success',
                                            onClose:function (){
                                                window.location.href = res.data.url
                                            }
                                        })
                                        // layer.msg(res.msg,{timeout:1000},function (){
                                        //     window.location.href = res.data.url
                                        // })
                                    }else{
                                        that.$message({
                                            message: res.msg?res.msg:"操作失败",
                                            type: 'error'
                                        })

                                    }
                                })
                            } else {
                                return false;
                            }
                        })
                    }
                }
            })
            common.loadELEMENT(app)
            app.mount("#app");
        },
        index:function () {
            var app = Vue.createApp({
                data() {
                    return {
                        activeName:"",
                        config: Config,
                        site: Config.site,
                        isCollapse: false,
                        asideBgColor: "#9f1414",
                        drawer: false,
                        direction: "rtl",
                        all_menu: [],
                        main_config: [],
                        select_main: {},
                        userinfo:null,
                        iframe_url: "",
                        default_active: 1,
                        setXh: {
                            index_background_color: "",
                            index_font_color: "",
                            left_action_font_color: "",
                        },
                        themeList: [
                            {
                                id: "white",
                                title: "白色主题",
                                index_background_color: "#ffffff",
                                index_font_color: "#000000",
                                left_action_font_color: "#626aef",
                            },
                            {
                                id: "black",
                                title: "黑色主题",
                                index_background_color: "#000000",
                                index_font_color: "#ffffff",
                                left_action_font_color: "#626aef"
                            }
                        ],

                    }
                },
                created() {
                    this.setXh = {
                        index_background_color: this.site.index_background_color,
                        index_font_color: this.site.index_font_color,
                        left_action_font_color: this.site.left_action_font_color
                    }
                    let kzt = {
                        title: "控制台",
                        key: 1,
                        url: "dashboard/index",
                        module: Config.moduleurl.replace(/^\//, '')
                    }
                    this.main_config.push(kzt)
                    this.select_main = kzt
                    if (Config.referer) {
                        let tem = {
                            title: Config.referer.title,
                            key: Config.referer.key,
                            url: Config.referer.controller + '/' + Config.referer.action,
                            module: Config.referer.module
                        }
                        // console.log(tem,'tttttttttttttttttttttt')
                        this.main_config.push(tem)
                        this.select_main = tem
                        // this.default_active = Config.referer.key
                        // console.log()
                    }
                    this.selectMainUrl()
                    this.getAllMenus()
                    this.getUserInfo()
                    window.getAllMenus = this.getAllMenus
                    window.getUserInfo = this.getUserInfo
                },
                methods: {
                    ...common,
                    tabsChange(e){
                      let main_config = this.main_config;
                      let that = this;
                      main_config.forEach(item=>{
                          if(item.key==e){
                              that.setRoute(`/`+item.module+`/`+item.url)
                          }
                      })
                    },
                    removeTab(row){
                        // console.log(row,11111111111111111)
                        // let main_config = this.main_config;
                        // let index = main_config.findIndex(item=>item.key==row.key)
                        this.delMain(row)
                    },
                    getUserInfo(){
                        let url = 'get_user_info';
                        let that = this;
                        $.get(url,function (res){
                            if(res.code==1){
                                that.userinfo = res.data
                            }
                        })
                    },
                    getAllMenus(){
                        // console.log('---------------------')
                        let url = 'get_menu';
                        let that = this;
                        $.get(url,function (res){
                            if(res.code==1){
                                that.all_menu = res.data
                            }
                        })
                    },
                    showGeneral() {
                        // console.log(Config,'cccccccccccc')
                        let tem = {
                            title: '个人资料',
                            name: "general/profile",
                            url: 'general.profile/index',
                            key: 8,
                            module: Config.moduleurl
                        }
                        let main_config = this.main_config;
                        let has = false;
                        main_config.forEach((item) => {
                            if (item.key == tem.key) {
                                this.select_main = item
                                has = true
                            }
                        })
                        if (has === false) {
                            this.main_config.push(tem)
                            this.select_main = tem
                        }
                        this.selectMainUrl()
                    },
                    delMain(key) {
                        console.log(key)
                        let main_config = this.main_config;
                        let index = 0;
                        main_config.forEach((item, indexs) => {
                            if (item.key==key) {
                                index = indexs
                            }
                        })
                        // console.log(index,'config')
                        if (this.select_main.key === key) {
                            if (this.main_config[index - 1]) {
                                this.select_main = this.main_config[index - 1]
                            }
                        }
                        this.main_config.splice(index, 1)
                        this.selectMainUrl()
                    },
                    handleClose() {
                        this.drawer = false
                    },
                    loadIframe(obj) {
                        this.select_main = obj
                        this.selectMainUrl()
                    },
                    //修改主题
                    changeTheme(e) {
                        let themeList = this.themeList;
                        themeList.forEach((item) => {
                            if (item.id == e) {
                                this.setXh = item
                            }
                        })
                    },
                    //
                    onSubmitTemp() {
                        //index_background_color
                        this.site.index_background_color = this.setXh.index_background_color
                        this.site.index_font_color = this.setXh.index_font_color
                        this.site.left_action_font_color = this.setXh.left_action_font_color
                    },
                    onSubmitSave() {
                        let url = this.config.moduleurl + "/general.config/save"
                        $.post(url, {row: this.setXh}, function (res) {
                            window.location.reload()
                        })
                    },
                    logout() {
                        let url = this.config.moduleurl + "/index/logout"
                        let that = this
                        layer.confirm("确定退出登录吗？", {
                            btn: ['确定', '取消'],
                            icon: 3,
                            title: "提示"
                        }, function (index) {
                            layer.close(index)
                            that.get(url, {}, function (res) {
                                window.location.href = that.config.moduleurl + '/' + res.url
                            })
                            // window.location.href = url
                        })
                    },
                    addMain(item){
                        if(item.route){
                            let route = item.route
                            if(route.indexOf('?')==-1){
                                route = route+'?ref=addtabs'
                            }else{
                                route = route+'&ref=addtabs'
                            }
                            let main_config = this.main_config;
                            let that = this
                            if(main_config.length>0){
                                let has = false
                                main_config.forEach((child)=>{
                                    if(child.key==item.id){
                                        that.select_main = {
                                            title: item.title,
                                            url:route,
                                            key:item.id,
                                            module:item.module
                                        }
                                        has = true
                                    }
                                })
                                console.log(has)
                                if(has){
                                    this.selectMainUrl()
                                    return
                                }
                            }
                            this.main_config.push({
                                title: item.title,
                                url:route,
                                key:item.id,
                                module:item.module
                            })
                            this.select_main = {
                                title: item.title,
                                url:route,
                                key:item.id,
                                module:item.module
                            }
                            this.selectMainUrl()
                        }
                    }
                }
            })
            common.loadELEMENT(app,['icon'])
            
            app.component('menu-item', {
                template: `
        <el-sub-menu
          v-if="item.children && item.children.length > 0"
          :index="item.id"
        >
          <template #title>
          <div  @click="addMains(item)">
          <el-icon><component :is="item.icon" /></el-icon>
          <span>{{ item.title }}</span>
</div>
            
          </template>
          <menu-item
            v-for="child in item.children"
            :key="child.id"
            :item="child"
            @add-main="addMains"
          />
        </el-sub-menu>
        <el-menu-item v-else :index="item.id" @click="addMains(item)">
         <el-icon><component :is="item.icon" /></el-icon>
          <span >{{ item.title }}</span>
        </el-menu-item>
      `,
                props: ['item'],
                methods:{
                    addMains(item){
                        // let href = location.href
                        // href = href.split('?')[0]
                        // if(href.indexOf('#')>-1){
                        //     href = href.split('#')[0]
                        // }
                        // if(item.route){
                        //     this.setRoute(item.route)
                        //     // href = href+'#'+Config.moduleurl+'/'+item.route
                        //     // console.log(href)
                        //     // history.replaceState({ page: href }, '', href);
                        // }
                        this.$emit('add-main',item)
                        // console.log(title,module,route,id,'-----------------------')
                    },
                    // setRoute(route){
                    //     let href = location.href
                    //     href = href.split('?')[0]
                    //     if(href.indexOf('#')>-1){
                    //         href = href.split('#')[0]
                    //     }
                    //     if(route){
                    //         href = href+'#'+Config.moduleurl+'/'+route
                    //         // console.log(href)
                    //         history.replaceState({ page: href }, '', href);
                    //     }
                    // }
                }
            });
            app.mount("#app");
            //<el-icon><component :is="item.meta.icon" /></el-icon>
        },
        get_icon: function () {
            var app = Vue.createApp({
                data(){
                    return {
                        selectIcon:"",
                        size:20,
                        allIcon:[]
                    }
                },
                created() {
                    // let that = this
                    for (const [key, component] of Object.entries(ElementPlusIconsVue)){
                        // console.log(key,'key')
                        this.allIcon.push(key);
                    }
                },
                methods:{
                    ...common,
                    baocun(icon){
                        console.log(icon,'iiiiiiiiiiiiiiiiiiiiiiiiii')
                        this.selectIcon = icon;
                        // console.log(window.parent)
                        window.parent.callback(icon)
                        let index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    }
                }
            })
            common.loadELEMENT(app)
            app.mount("#app");
        }
    }
    return Controller
})